<?php

$mcrp = './';
include_once('functions/session.php');
include_once('functions/nanofight.php');
include_once('functions/users.php');

$head = '<link href="nano/nano.css" rel="stylesheet" media="screen" type="text/css" title="style" />
<script type="text/javascript" src="nano/nanofight.js"></script>';
include($mcrp . 'skin/default.php');
include($mcrp . 'skin/header.php');

if(isset($_POST['free']))
{
	if(isset($_POST['playercount']) && is_numeric($_POST['playercount']) && (int)$_POST['playercount'] == $_POST['playercount'])
	{	
		CreateFightFree($_POST['playercount']);
		header('Location: nanofight.php');
		exit;
	}
}
else if(isset($_POST['private']) && isset($_POST['players']) && is_array($_POST['players']))
{
	if(count($_POST['players']) > 7) $err = 'Vous devez sélectionner au maximum 7 joueurs.';
	else
	{
		$bOk = true;
		foreach($_POST['players'] as $p) if(!is_numeric($p) || (int)$p != $p) $bOk = false;
		if($bOk == false) $err = 'Erreur de formatage.';
		else { CreateFightPrivate($_POST['players']); header('Location: nanofight.php'); exit; }
	}
}

$users = GetAllUsers('points DESC');

?>
<div class="mc_box">
	<h3><a href="nano.php">Nanocontest</a> :: <a href="nanofight.php">Combats</a> :: Nouveau</h3>
	<?php if($mcuser->id == 0) { ?>
	<div><center><img src="img/stop-trackback.png"><br/>Vous devez être connecté pour accéder à cette page.<br />Si vous n'êtes pas encore inscrit, rendez-vous sur la page d'<a href="register.php">inscription</a>.</center></div>
	<?php } else { ?>
	<div class="mc_box_content">
		<div class="nc_button_left" style="margin-bottom:0;" onclick="NanoCreateFightChange('nc_fight_free');">Inscriptions libres</div><div class="nc_button_right" style="margin-bottom:0;" onclick="NanoCreateFightChange('nc_fight_private');">Défier des joueurs</div>
		<?php if($mcuser->isadmin) { ?><div class="nc_button" style="margin-bottom:0;" onclick="NanoCreateFightChange('nc_fight_special');">Spécial</div><?php } ?>
		<div style="margin:-1px 8px 20px 8px;border:1px solid rgba(0,0,0,.5);padding:15px;">
		<div id="nc_fight_free">
			<form method="post">
				Nombre de joueurs : <select name="playercount"><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option></select>
				<input class="nc_button" type="submit" name="free" value="Créer" />
			</form>
		</div>
		<div id="nc_fight_private" style="display:none;">
			Sélectionner les joueurs que vous désirez affronter (touche CTRL enfoncée pour sélectionner plusieurs joueurs) :
			<form method="post">
				<select name="players[]" multiple="multiple" size="15">
					<?php
					foreach($users as $u)
					{
						if($u->id != $mcuser->id) echo '<option value="' . $u->id . '">' . $u->name . '</option>';
					}
					?>
				</select>
				<input class="nc_button" type="submit" name="private" value="Créer" />
			</form>
		</div>
		<?php if($mcuser->isadmin) { ?>
		<div id="nc_fight_special" style="display:none;">
			Spécial
		</div>
		<?php } ?>
		</div>
	</div>
	<?php } ?>
</div>
<?php

include($mcrp . 'skin/footer.php');

?>